package cn.ecnu.controller;

import cn.ecnu.common.utils.JwtToken;
import cn.ecnu.common.utils.JwtUtil;
import cn.ecnu.common.utils.R;
import cn.ecnu.vo.fromFront.ConsultantForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    RedisTemplate<Object, Object> template;


    @GetMapping("/hello")
    public R hello() {
        return R.ok();
    }

    @JwtToken
    @GetMapping("/hi")
    public R hi(@RequestAttribute("userId") Integer userId) {
        return R.ok().put("id", userId);
    }

    @GetMapping("/token")
    public R token() {
        return R.ok().put("token", jwtUtil.sign(1));
    }

    @GetMapping("/redis")
    public R redis() {
        template.opsForValue().set("dad:asd", R.ok());
        return R.ok();
    }

    @PostMapping("/jsr")
    public R jsr(@Validated @RequestBody ConsultantForm form) {
        return R.ok().put("data", form);
    }
}
